package com.eduhzy.ttw.teacher.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.StringUtils;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.EventBusTags;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.entity.RegisterRequestData;
import com.eduhzy.ttw.commonsdk.utils.PatternUtil;
import com.eduhzy.ttw.teacher.R;
import com.eduhzy.ttw.teacher.di.component.DaggerRegisterComponent;
import com.eduhzy.ttw.teacher.mvp.contract.RegisterContract;
import com.eduhzy.ttw.teacher.mvp.presenter.RegisterPresenter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.xw.repo.XEditText;
import com.zhy.autolayout.AutoLinearLayout;

import org.simple.eventbus.Subscriber;

import java.lang.ref.Reference;
import java.lang.ref.WeakReference;

import butterknife.BindView;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;

import static com.eduhzy.ttw.commonsdk.core.Constants.LOGIN_THIRD;
import static com.eduhzy.ttw.commonsdk.core.Constants.PHONE_VERIFY;
import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * @author xubo
 */
@Route(path = RouterHub.APP_REGISTERACTIVITY)
public class RegisterActivity extends BaseActivity<RegisterPresenter> implements RegisterContract.View {
    @Autowired(name = Intent.EXTRA_TITLE)
    String title;
    @Autowired(name = Constants.LOGIN_TYPE)
    int loginType = 0;
    @Autowired(name = Constants.OPENID)
    String openid;
    @BindView(R.id.register_name)
    XEditText registerName;

    @BindView(R.id.register_user)
    XEditText registerUser;
    @BindView(R.id.register_pwd)
    XEditText registerPwd;
    @BindView(R.id.register_pwd_layout)
    AutoLinearLayout registerPwdLayout;
    RegisterRequestData data;
    private Reference<LoginActivity> reference;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerRegisterComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_register; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        if (loginType == 0) throw new NullPointerException("请在Intent中添加Constants.LOGIN_TYPE");
        if (loginType != 3) {
            registerPwdLayout.setVisibility(View.GONE);
        }
        reference = new WeakReference(this);
        data = new RegisterRequestData();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @OnCheckedChanged({R.id.register_male, R.id.register_female})
    public void onCheckedChanged(CompoundButton button, boolean checked) {
        if (checked) {
            switch (button.getId()) {
                case R.id.register_male:
                    data.setGender("1");
                    break;
                case R.id.register_female:
                    data.setGender("2");
                    break;
                default:
            }
        }
    }

    @OnClick(R.id.register_next)
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.register_next:
                KeyboardUtils.hideSoftInput(reference.get());
                String name = registerName.getText().toString();
                String user = registerUser.getText().toString();
                String pwd = registerPwd.getText().toString();

                if (StringUtils.isTrimEmpty(name)) {
//                    showMessage("请输入您的姓名!");
                    RxUtil.showTipDialog(this, QMUITipDialog.Builder.ICON_TYPE_FAIL, "请输入您的姓名!", 1500);
                    return;
                }
                if (StringUtils.isTrimEmpty(user) || !PatternUtil.isMobilePhone(user)) {
                    RxUtil.showTipDialog(this, QMUITipDialog.Builder.ICON_TYPE_FAIL, "请输入正确的手机号码!", 1500);
                    return;
                }
                if (loginType == 3) {
                    if (StringUtils.isTrimEmpty(pwd) || !PatternUtil.isContainNumAndLetter(pwd)) {
                        RxUtil.showTipDialog(this, QMUITipDialog.Builder.ICON_TYPE_FAIL, "密码必须同时包含字母和数字,且最少有8位!", 3000);
                        return;
                    }
                }
                switch (loginType) {
                    case 3:
                        data.setYddh(user);
                        data.setPassWord(pwd);
                        data.setRealname(name);
                        break;
                    case LOGIN_THIRD:
                        data.setDeviceId(SPUtils.getInstance().getString(Constants.DEVICEID));
                        data.setRealName(name);
                        data.setUserNameType(String.valueOf(Constants.LOGIN_THIRD_QQ));
                        data.setMobile(user);
                        data.setOpenid(openid);
                        break;
                    default:
                }
                data.setType(Constants.CLIENT_TYPE);
                if (ObjectUtils.isEmpty(data.getGender())) {
                    showMessage("请选择您的性别");
                    RxUtil.showTipDialog(this, QMUITipDialog.Builder.ICON_TYPE_FAIL, "请选择您的性别", 1500);
                    return;
                }
                ARouter.getInstance()
                        .build(RouterHub.APP_PHONEVERIFYACTIVITY)
                        .withString(Constants.USERNAME, user)
                        .withString(Intent.EXTRA_TITLE, PHONE_VERIFY)
                        .withString(Constants.TEMPLATE, loginType == Constants.LOGIN_THIRD ? "5" : Constants.TYPE_REGISTER)
                        .navigation();
                break;
            default:
        }
    }

    @Subscriber(tag = RouterHub.APP_REGISTERACTIVITY)
    public void onEvents(Message msg) {
        switch (msg.what) {
            case EventBusTags.GET_CODE_SUCCESS:
                data.setCode((String) msg.obj);
                if (data.getCode() != null) {
                    mPresenter.requestRegister(data, loginType);
                }
                break;
            default:
        }
    }

    @Override
    public Activity getActivity() {
        return this;
    }
}
